package com.applink.sell_proof;

import java.util.Observable;
import java.util.Observer;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.dialog.WaitingDialog;

public class LogIn extends DialogFragment implements OnClickListener, Observer {

	public static final String TAG = "Login";

	private static final int LOGIN = 0;
	private static final int LOGIN_OFFLINE = 1;
	private String email = "";
	private String pass = "";

	private Handler mHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message arg0) {
			switch (arg0.what) {
			case LOGIN:
				DataHelper.getInstance().login(email, pass);
				break;

			case LOGIN_OFFLINE:
				if (email.equals(EMarketing.getInstance().getAccountEmail())
						&& pass.equals(EMarketing.getInstance()
								.getAccountPassword())) {
					EMarketing.getInstance().setLogin(true);
					Splash.getInstant.mHandler
							.sendEmptyMessage(Splash.LOAD_DATA_SYNC_OFFLINE);

					// startActivity(new Intent(getActivity(),
					// AllProjectActivity.class));
					// getActivity().finish();
				}
			default:
				break;
			}
			return false;
		}
	});

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(STYLE_NO_FRAME, 0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.login, null, false);
		// ((TextView) view.findViewById(R.id.login_email)).setText(EMarketing
		// .getInstance().getAccountEmail());
		view.findViewById(R.id.btn_login).setOnClickListener(this);
		view.findViewById(R.id.forget_password).setOnClickListener(this);
		return view;
	}

	@Override
	public void onResume() {
		super.onResume();
		DataHelper.getInstance().addObserver(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		DataHelper.getInstance().deleteObserver(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_login) {

			toggleLoading(true);

			// email = ((TextView) getView().findViewById(R.id.login_email))
			// .getText().toString();
			// pass = ((TextView) getView().findViewById(R.id.login_pass))
			// .getText().toString();
			if (!EMarketing.isOnline()) {
				mHandler.sendEmptyMessage(LOGIN_OFFLINE);
			}

			else {
				// mHandler.sendEmptyMessageDelayed(LOGIN, 10000);
				mHandler.sendEmptyMessage(LOGIN);
			}

			// DataHelper.getInstance().login(email, pass);
		} else if (v.getId() == R.id.forget_password) {
			// (new ForgotPassword()).show(getActivity()
			// .getSupportFragmentManager(), ChangePassword.TAG);
			// dismiss();
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// if (data == null)
		// return;
		// DataResult result = (DataResult) data;
		// if (result.getMethodName() == Methods.Login) {
		// toggleLoading(false);
		// if (result.getErrorCode() == null) {
		// int responseCode = (Integer) result.getData();
		// if (responseCode == 0) {
		// getView().findViewById(R.id.login_error).setVisibility(
		// View.VISIBLE);
		// } else if (responseCode == 1) {
		// // EMarketing.getInstance().setAccountEmail(
		// // ((TextView) getView()
		// // .findViewById(R.id.login_email)).getText()
		// // .toString());
		// // EMarketing.getInstance().setAccountPassword(
		// // ((TextView) getView()
		// // .findViewById(R.id.login_pass)).getText()
		// // .toString());
		// Log.e("abc", "token "
		// + EMarketing.getInstance().getAccountToken());
		// Log.e("abc", "contact id "
		// + EMarketing.getInstance().getContactID());
		// DataHelper.getInstance().getUserSetting();
		// DataHelper.getInstance().getAllCountry();
		// final String regId = GCMRegistrar
		// .getRegistrationId(getActivity()
		// .getApplicationContext());
		// Utility.SyncData();
		// if (regId.equals("")) {
		// // Automatically registers application on startup.
		// GCMRegistrar.register(getActivity()
		// .getApplicationContext(), Config.GCM_SENDER_ID);
		// }
		// // startActivity(new Intent(getActivity(), ClaimActivity.class));
		// // getActivity().finish();
		// } else if (responseCode == 2) {
		// // DialogFragment changePass = ChangePassword
		// // .getInstance(((TextView) getView().findViewById(
		// // R.id.login_email)).getText().toString());
		// // changePass.show(getActivity().getSupportFragmentManager(),
		// // ChangePassword.TAG);
		// dismiss();
		// }
		// } else {
		// getView().findViewById(R.id.login_error).setVisibility(
		// View.VISIBLE);
		// }
		// }
	}

	private void toggleLoading(boolean show) {
		DialogFragment loading = (DialogFragment) getActivity()
				.getSupportFragmentManager().findFragmentByTag(
						WaitingDialog.TAG);
		if (show) {
			if (loading != null)
				loading.show(getFragmentManager(), WaitingDialog.TAG);
			else {
				loading = new WaitingDialog(android.R.color.transparent);
				loading.show(getFragmentManager(), WaitingDialog.TAG);
			}
		} else if (loading != null) {
			loading.dismiss();
		}
	}

}
